/**
 * Created with JetBrains WebStorm.
 * User: smd
 * Date: 2/10/14
 * Time: 8:02 AM
 * To change this template use File | Settings | File Templates.
 */

function ReportVO(rawData, fullComponentsList) {

    this.rawData = rawData;
    this.columns = [];
    this.rows = [];
    this.rowsFull = [];
    this.metaData;
    this.groupedList = [];

    this.groupByKey = 'ZEHFIRKPI__ZEHFIRKPI_KEY';
    this.groupByText = 'ZEHFIRKPI__ZEHFIRKPI_TXT';
    this.fullComponentsList = fullComponentsList;


    ReportVO.prototype.parse = function(){

        if(this.rawData !== undefined)
        {
            if(this.rawData != 'undefined')
            {
                this.columns = this.parseColumns(this.rawData.report.header, fullComponentsList);
                this.metaData = this.parseMetaData(this.rawData.report.metadata);

                var dirtyRowList = this.parseRows(this.rawData.report.data);
                this.groupedList = this.getGroupList(dirtyRowList, this.groupByKey, this.groupByText);
                this.fullComponentsList = this.GetMergedGroups(this.groupedList, this.fullComponentsList);
                this.rows = this.removeRowsWithMain(dirtyRowList);
                this.rowsFull = this.GetRowsWithMissingComponents(this.groupedList, this.fullComponentsList, this.rows);


            }
        }
    }

    ReportVO.prototype.getGroupList = function(rows,groupByKey, groupByText)
    {
        var result = [];
        for(var i=0; i < rows.length; i++)
        {
            var data = rows[i];
            var temp = {};
            for (var prop in data) {
                if(prop == "ZFIRCOMP" && data['ZFIRCOMP'] == "MAIN")
                {
                    temp['value'] = data['VALUE001'];
                    temp['text'] = data[groupByText];
                    temp['id'] = data[groupByKey];
                    result.push(temp);
                }
            }
        }

        /* remove duplicates */
        result = unique(result, 'id');
        return result;
    }

    ReportVO.prototype.removeRowsWithMain = function(rows) {
        var result = [];

        for( var x = 0 ; x < rows.length; x++)
        {
            if(rows[x]['ZFIRCOMP'] != "MAIN")
            {
                result.push(rows[x]);
            }
        }
        return result;
    }

    ReportVO.prototype.parseMetaData = function(rawJson){
        var result = new ReportMetaDataVO(rawJson);
        return result;
    }

    ReportVO.prototype.parseRows = function(rawJson){
        var result = [];
        if(rawJson != undefined)
        {
            for(var i=0; i < rawJson.length; i++)
            {
                var row = new ReportRowVO(rawJson[i].row.data);
                result.push(row);
            }
        }
        return result;
    }


    ReportVO.prototype.addMissingColumns = function(columnsList){
        var column = {};
        column['caption'] = 'Target';
        column['datatype'] = 'CHAR';
        column['length'] = '000025';
        column['techname'] = 'target';
        column['type'] = 'CHA';
        columnsList.push(column);

        return columnsList;
    }

    ReportVO.prototype.parseColumns = function(rawJson, fullComponentsList){
        var result = [];
        if(rawJson != undefined)
        {
            for(var i=0; i < rawJson.length; i++)
            {
                var column = new ReportColumnVO(rawJson[i].column);
                result.push(column);
            }
        }

        result = this.addMissingColumns(result);

        return result;
    }

    ReportVO.prototype.GetMergedGroups = function(groupedList, fullComponentsList) {
        for(var x = 0; x < groupedList.length; x++)
        {
            for(var i = 0; i < fullComponentsList.length; i++)
            {
                if(groupedList[x].id === fullComponentsList[i].id)
                {
                    var target = fullComponentsList[i]['target'];
                    fullComponentsList[i] = groupedList[x];
                    fullComponentsList[i]['target'] = target;

                }
            }
        }
        return fullComponentsList;
    }

    ReportVO.prototype.GetRowsWithMissingComponents = function(groupedList, fullComponentsList, rows) {
       var missingComponents = this.GetMissingComponents(groupedList, fullComponentsList);
       var rowsWithMissingComponent =  this.CreateRowsForMissingComponents(rows, missingComponents);
        this.AddTargetValueToRows(rowsWithMissingComponent, groupedList);
        return rowsWithMissingComponent;

    }

    ReportVO.prototype.AddTargetValueToRows = function (rows,  groupedList)
    {
        for( var i=0; i < this.rows.length; i++ )
        {
            for(var x = 0; x < this.groupedList.length; x++)
            {
                if( this.rows[i]['ZFIRCOMP'] === this.groupedList[x]['id'])
                {
                    if(this.groupedList[x].hasOwnProperty('target'))
                       this.rows[i]['target'] = this.groupedList[x]['target'];
                    else
                        this.rows[i]['target'] = '';
                }
            }
        }
        return rows;
    }
    ReportVO.prototype.CreateRowsForMissingComponents = function(rows, missingComponents)  {

        for(var i=0; i < missingComponents.length; i++)
        {
            var sampleRow = {};
            sampleRow.ZEHFIRKPI__ZEHFIRKPI_KEY = "";
            sampleRow.ZEHFIRKPI__ZEHFIRKPI_TXT = "";
            sampleRow.ORGUNIT = '';
            sampleRow.CALQUARTER = '';
            sampleRow.CALYEAR = '';
            sampleRow.CALQUART1 = '';
            sampleRow.VALUE001 = '';
            sampleRow.ZFIRCOMP = missingComponents[i].id;
            rows.push(sampleRow);
        }

        var sampleRow = {};
        sampleRow.ZEHFIRKPI__ZEHFIRKPI_KEY = "";
        sampleRow.ZEHFIRKPI__ZEHFIRKPI_TXT = "";
        sampleRow.ORGUNIT = '';
        sampleRow.CALQUARTER = '';
        sampleRow.CALYEAR = '';
        sampleRow.CALQUART1 = '';
        sampleRow.VALUE001 = '';
        sampleRow.ZFIRCOMP = missingComponents[i-1].id;
        rows.push(sampleRow);
        return rows;
    }

    ReportVO.prototype.GetMissingComponents = function(groupedList, fullComponentsList)
    {
       var result = [];
       /*for(var i=0; i < fullComponentsList.length; i++)*/
       for(var i=0; i < fullComponentsList.length; i++)
       {
           var isFound = false;
           for(var x = 0; x < groupedList.length; x++)
           {
               if(groupedList[x].id ===  fullComponentsList[i].id)
               {
                   isFound = true;
                   break
               }
           }
           if(!isFound)
           {
               result.push(fullComponentsList[i]);
           }
       }
        return result;
    }

}


function ReportMetaDataVO(data) {
    if(data != 'undefined')
    {
        for (var prop in data) {
            this[prop] = data[prop];
        }
    }
}

function ReportColumnVO(data) {
    if(data != 'undefined')
    {
        for (var prop in data) {
            this[prop] = data[prop];
        }
    }
}

function ReportRowVO(data) {
  if(data != 'undefined')
  {
      for (var prop in data) {
          this[prop] = data[prop];
      }
  }
}



